home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / _abcoll.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  21KB  |  692 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Abstract Base Classes (ABCs) for collections, according to PEP 3119.
  5.  
  6. DON'T USE THIS MODULE DIRECTLY!  The classes here should be imported
  7. via collections; they are defined here only to alleviate certain
  8. bootstrapping issues.  Unit tests are in test_collections.
  9. """
  10. from abc import ABCMeta, abstractmethod
  11. import sys
  12. __all__ = [
  13.     'Hashable',
  14.     'Iterable',
  15.     'Iterator',
  16.     'Sized',
  17.     'Container',
  18.     'Callable',
  19.     'Set',
  20.     'MutableSet',
  21.     'Mapping',
  22.     'MutableMapping',
  23.     'MappingView',
  24.     'KeysView',
  25.     'ItemsView',
  26.     'ValuesView',
  27.     'Sequence',
  28.     'MutableSequence']
  29.  
  30. class Hashable:
  31.     __metaclass__ = ABCMeta
  32.     
  33.     def __hash__(self):
  34.         return 0
  35.  
  36.     __hash__ = abstractmethod(__hash__)
  37.     
  38.     def __subclasshook__(cls, C):
  39.         if cls is Hashable:
  40.             for B in C.__mro__:
  41.                 if '__hash__' in B.__dict__:
  42.                     if B.__dict__['__hash__']:
  43.                         return True
  44.                     break
  45.                     continue
  46.                 B.__dict__['__hash__']
  47.             
  48.         
  49.         return NotImplemented
  50.  
  51.     __subclasshook__ = classmethod(__subclasshook__)
  52.  
  53.  
  54. class Iterable:
  55.     __metaclass__ = ABCMeta
  56.     
  57.     def __iter__(self):
  58.         while False:
  59.             yield None
  60.  
  61.     __iter__ = abstractmethod(__iter__)
  62.     
  63.     def __subclasshook__(cls, C):
  64.         if cls is Iterable:
  65.             if any((lambda .0: for B in .0:
  66. '__iter__' in B.__dict__)(C.__mro__)):
  67.                 return True
  68.         
  69.         return NotImplemented
  70.  
  71.     __subclasshook__ = classmethod(__subclasshook__)
  72.  
  73. Iterable.register(str)
  74.  
  75. class Iterator(Iterable):
  76.     
  77.     def next(self):
  78.         raise StopIteration
  79.  
  80.     next = abstractmethod(next)
  81.     
  82.     def __iter__(self):
  83.         return self
  84.  
  85.     
  86.     def __subclasshook__(cls, C):
  87.         if cls is Iterator:
  88.             if any((lambda .0: for B in .0:
  89. 'next' in B.__dict__)(C.__mro__)):
  90.                 return True
  91.         
  92.         return NotImplemented
  93.  
  94.     __subclasshook__ = classmethod(__subclasshook__)
  95.  
  96.  
  97. class Sized:
  98.     __metaclass__ = ABCMeta
  99.     
  100.     def __len__(self):
  101.         return 0
  102.  
  103.     __len__ = abstractmethod(__len__)
  104.     
  105.     def __subclasshook__(cls, C):
  106.         if cls is Sized:
  107.             if any((lambda .0: for B in .0:
  108. '__len__' in B.__dict__)(C.__mro__)):
  109.                 return True
  110.         
  111.         return NotImplemented
  112.  
  113.     __subclasshook__ = classmethod(__subclasshook__)
  114.  
  115.  
  116. class Container:
  117.     __metaclass__ = ABCMeta
  118.     
  119.     def __contains__(self, x):
  120.         return False
  121.  
  122.     __contains__ = abstractmethod(__contains__)
  123.     
  124.     def __subclasshook__(cls, C):
  125.         if cls is Container:
  126.             if any((lambda .0: for B in .0:
  127. '__contains__' in B.__dict__)(C.__mro__)):
  128.                 return True
  129.         
  130.         return NotImplemented
  131.  
  132.     __subclasshook__ = classmethod(__subclasshook__)
  133.  
  134.  
  135. class Callable:
  136.     __metaclass__ = ABCMeta
  137.     
  138.     def __call__(self, *args, **kwds):
  139.         return False
  140.  
  141.     __call__ = abstractmethod(__call__)
  142.     
  143.     def __subclasshook__(cls, C):
  144.         if cls is Callable:
  145.             if any((lambda .0: for B in .0:
  146. '__call__' in B.__dict__)(C.__mro__)):
  147.                 return True
  148.         
  149.         return NotImplemented
  150.  
  151.     __subclasshook__ = classmethod(__subclasshook__)
  152.  
  153.  
  154. class Set(Sized, Iterable, Container):
  155.     '''A set is a finite, iterable container.
  156.  
  157.     This class provides concrete generic implementations of all
  158.     methods except for __contains__, __iter__ and __len__.
  159.  
  160.     To override the comparisons (presumably for speed, as the
  161.     semantics are fixed), all you have to do is redefine __le__ and
  162.     then the other operations will automatically follow suit.
  163.     '''
  164.     
  165.     def __le__(self, other):
  166.         if not isinstance(other, Set):
  167.             return NotImplemented
  168.         if len(self) > len(other):
  169.             return False
  170.         for elem in self:
  171.             if elem not in other:
  172.                 return False
  173.         
  174.         return True
  175.  
  176.     
  177.     def __lt__(self, other):
  178.         return isinstance(other, Set) if not isinstance(other, Set) else self.__le__(other)
  179.  
  180.     
  181.     def __gt__(self, other):
  182.         if not isinstance(other, Set):
  183.             return NotImplemented
  184.         return other < self
  185.  
  186.     
  187.     def __ge__(self, other):
  188.         if not isinstance(other, Set):
  189.             return NotImplemented
  190.         return other <= self
  191.  
  192.     
  193.     def __eq__(self, other):
  194.         return isinstance(other, Set) if not isinstance(other, Set) else self.__le__(other)
  195.  
  196.     
  197.     def __ne__(self, other):
  198.         return not (self == other)
  199.  
  200.     
  201.     def _from_iterable(cls, it):
  202.         '''Construct an instance of the class from any iterable input.
  203.  
  204.         Must override this method if the class constructor signature
  205.         does not accept an iterable for an input.
  206.         '''
  207.         return cls(it)
  208.  
  209.     _from_iterable = classmethod(_from_iterable)
  210.     
  211.     def __and__(self, other):
  212.         if not isinstance(other, Iterable):
  213.             return NotImplemented
  214.         return (self._from_iterable,)((lambda .0: for value in .0:
  215. if value in self:
  216. valuecontinue)(other))
  217.  
  218.     
  219.     def isdisjoint(self, other):
  220.         for value in other:
  221.             if value in self:
  222.                 return False
  223.         
  224.         return True
  225.  
  226.     
  227.     def __or__(self, other):
  228.         if not isinstance(other, Iterable):
  229.             return NotImplemented
  230.         chain = (lambda .0: for s in .0:
  231. for e in s:
  232. e)((self, other))
  233.         return self._from_iterable(chain)
  234.  
  235.     
  236.     def __sub__(self, other):
  237.         if not isinstance(other, Set):
  238.             if not isinstance(other, Iterable):
  239.                 return NotImplemented
  240.             other = self._from_iterable(other)
  241.         
  242.         return (self._from_iterable,)((lambda .0: for value in .0:
  243. if value not in other:
  244. valuecontinue)(self))
  245.  
  246.     
  247.     def __xor__(self, other):
  248.         if not isinstance(other, Set):
  249.             if not isinstance(other, Iterable):
  250.                 return NotImplemented
  251.             other = self._from_iterable(other)
  252.         
  253.         return self - other | other - self
  254.  
  255.     __hash__ = None
  256.     
  257.     def _hash(self):
  258.         """Compute the hash value of a set.
  259.  
  260.         Note that we don't define __hash__: not all sets are hashable.
  261.         But if you define a hashable set type, its __hash__ should
  262.         call this function.
  263.  
  264.         This must be compatible __eq__.
  265.  
  266.         All sets ought to compare equal if they contain the same
  267.         elements, regardless of how they are implemented, and
  268.         regardless of the order of the elements; so there's not much
  269.         freedom for __eq__ or __hash__.  We match the algorithm used
  270.         by the built-in frozenset type.
  271.         """
  272.         MAX = sys.maxint
  273.         MASK = 2 * MAX + 1
  274.         n = len(self)
  275.         h = 1927868237 * (n + 1)
  276.         h &= MASK
  277.         for x in self:
  278.             hx = hash(x)
  279.             h ^= (hx ^ hx << 16 ^ 89869747) * 0xD93F34D7L
  280.             h &= MASK
  281.         
  282.         h = h * 69069 + 907133923
  283.         h &= MASK
  284.         if h > MAX:
  285.             h -= MASK + 1
  286.         
  287.         if h == -1:
  288.             h = 590923713
  289.         
  290.         return h
  291.  
  292.  
  293. Set.register(frozenset)
  294.  
  295. class MutableSet(Set):
  296.     
  297.     def add(self, value):
  298.         '''Add an element.'''
  299.         raise NotImplementedError
  300.  
  301.     add = abstractmethod(add)
  302.     
  303.     def discard(self, value):
  304.         '''Remove an element.  Do not raise an exception if absent.'''
  305.         raise NotImplementedError
  306.  
  307.     discard = abstractmethod(discard)
  308.     
  309.     def remove(self, value):
  310.         '''Remove an element. If not a member, raise a KeyError.'''
  311.         if value not in self:
  312.             raise KeyError(value)
  313.         value not in self
  314.         self.discard(value)
  315.  
  316.     
  317.     def pop(self):
  318.         '''Return the popped value.  Raise KeyError if empty.'''
  319.         it = iter(self)
  320.         
  321.         try:
  322.             value = next(it)
  323.         except StopIteration:
  324.             raise KeyError
  325.  
  326.         self.discard(value)
  327.         return value
  328.  
  329.     
  330.     def clear(self):
  331.         '''This is slow (creates N new iterators!) but effective.'''
  332.         
  333.         try:
  334.             while True:
  335.                 self.pop()
  336.         except KeyError:
  337.             pass
  338.  
  339.  
  340.     
  341.     def __ior__(self, it):
  342.         for value in it:
  343.             self.add(value)
  344.         
  345.         return self
  346.  
  347.     
  348.     def __iand__(self, it):
  349.         for value in self - it:
  350.             self.discard(value)
  351.         
  352.         return self
  353.  
  354.     
  355.     def __ixor__(self, it):
  356.         if not isinstance(it, Set):
  357.             it = self._from_iterable(it)
  358.         
  359.         for value in it:
  360.             if value in self:
  361.                 self.discard(value)
  362.                 continue
  363.             self.add(value)
  364.         
  365.         return self
  366.  
  367.     
  368.     def __isub__(self, it):
  369.         for value in it:
  370.             self.discard(value)
  371.         
  372.         return self
  373.  
  374.  
  375. MutableSet.register(set)
  376.  
  377. class Mapping(Sized, Iterable, Container):
  378.     
  379.     def __getitem__(self, key):
  380.         raise KeyError
  381.  
  382.     __getitem__ = abstractmethod(__getitem__)
  383.     
  384.     def get(self, key, default = None):
  385.         
  386.         try:
  387.             return self[key]
  388.         except KeyError:
  389.             return default
  390.  
  391.  
  392.     
  393.     def __contains__(self, key):
  394.         
  395.         try:
  396.             self[key]
  397.         except KeyError:
  398.             return False
  399.  
  400.         return True
  401.  
  402.     
  403.     def iterkeys(self):
  404.         return iter(self)
  405.  
  406.     
  407.     def itervalues(self):
  408.         for key in self:
  409.             yield self[key]
  410.         
  411.  
  412.     
  413.     def iteritems(self):
  414.         for key in self:
  415.             yield (key, self[key])
  416.         
  417.  
  418.     
  419.     def keys(self):
  420.         return list(self)
  421.  
  422.     
  423.     def items(self):
  424.         return [ (key, self[key]) for key in self ]
  425.  
  426.     
  427.     def values(self):
  428.         return [ self[key] for key in self ]
  429.  
  430.     __hash__ = None
  431.     
  432.     def __eq__(self, other):
  433.         if isinstance(other, Mapping):
  434.             pass
  435.         return dict(self.items()) == dict(other.items())
  436.  
  437.     
  438.     def __ne__(self, other):
  439.         return not (self == other)
  440.  
  441.  
  442.  
  443. class MappingView(Sized):
  444.     
  445.     def __init__(self, mapping):
  446.         self._mapping = mapping
  447.  
  448.     
  449.     def __len__(self):
  450.         return len(self._mapping)
  451.  
  452.  
  453.  
  454. class KeysView(MappingView, Set):
  455.     
  456.     def __contains__(self, key):
  457.         return key in self._mapping
  458.  
  459.     
  460.     def __iter__(self):
  461.         for key in self._mapping:
  462.             yield key
  463.         
  464.  
  465.  
  466.  
  467. class ItemsView(MappingView, Set):
  468.     
  469.     def __contains__(self, item):
  470.         (key, value) = item
  471.         
  472.         try:
  473.             v = self._mapping[key]
  474.         except KeyError:
  475.             return False
  476.  
  477.         return v == value
  478.  
  479.     
  480.     def __iter__(self):
  481.         for key in self._mapping:
  482.             yield (key, self._mapping[key])
  483.         
  484.  
  485.  
  486.  
  487. class ValuesView(MappingView):
  488.     
  489.     def __contains__(self, value):
  490.         for key in self._mapping:
  491.             if value == self._mapping[key]:
  492.                 return True
  493.         
  494.         return False
  495.  
  496.     
  497.     def __iter__(self):
  498.         for key in self._mapping:
  499.             yield self._mapping[key]
  500.         
  501.  
  502.  
  503.  
  504. class MutableMapping(Mapping):
  505.     
  506.     def __setitem__(self, key, value):
  507.         raise KeyError
  508.  
  509.     __setitem__ = abstractmethod(__setitem__)
  510.     
  511.     def __delitem__(self, key):
  512.         raise KeyError
  513.  
  514.     __delitem__ = abstractmethod(__delitem__)
  515.     __marker = object()
  516.     
  517.     def pop(self, key, default = __marker):
  518.         
  519.         try:
  520.             value = self[key]
  521.         except KeyError:
  522.             if default is self._MutableMapping__marker:
  523.                 raise 
  524.             default is self._MutableMapping__marker
  525.             return default
  526.  
  527.         del self[key]
  528.         return value
  529.  
  530.     
  531.     def popitem(self):
  532.         
  533.         try:
  534.             key = next(iter(self))
  535.         except StopIteration:
  536.             raise KeyError
  537.  
  538.         value = self[key]
  539.         del self[key]
  540.         return (key, value)
  541.  
  542.     
  543.     def clear(self):
  544.         
  545.         try:
  546.             while True:
  547.                 self.popitem()
  548.         except KeyError:
  549.             pass
  550.  
  551.  
  552.     
  553.     def update(self, other = (), **kwds):
  554.         if isinstance(other, Mapping):
  555.             for key in other:
  556.                 self[key] = other[key]
  557.             
  558.         elif hasattr(other, 'keys'):
  559.             for key in other.keys():
  560.                 self[key] = other[key]
  561.             
  562.         else:
  563.             for key, value in other:
  564.                 self[key] = value
  565.             
  566.         for key, value in kwds.items():
  567.             self[key] = value
  568.         
  569.  
  570.     
  571.     def setdefault(self, key, default = None):
  572.         
  573.         try:
  574.             return self[key]
  575.         except KeyError:
  576.             self[key] = default
  577.  
  578.         return default
  579.  
  580.  
  581. MutableMapping.register(dict)
  582.  
  583. class Sequence(Sized, Iterable, Container):
  584.     '''All the operations on a read-only sequence.
  585.  
  586.     Concrete subclasses must override __new__ or __init__,
  587.     __getitem__, and __len__.
  588.     '''
  589.     
  590.     def __getitem__(self, index):
  591.         raise IndexError
  592.  
  593.     __getitem__ = abstractmethod(__getitem__)
  594.     
  595.     def __iter__(self):
  596.         i = 0
  597.         
  598.         try:
  599.             while True:
  600.                 v = self[i]
  601.                 yield v
  602.                 i += 1
  603.         except IndexError:
  604.             return None
  605.  
  606.  
  607.     
  608.     def __contains__(self, value):
  609.         for v in self:
  610.             if v == value:
  611.                 return True
  612.         
  613.         return False
  614.  
  615.     
  616.     def __reversed__(self):
  617.         for i in reversed(range(len(self))):
  618.             yield self[i]
  619.         
  620.  
  621.     
  622.     def index(self, value):
  623.         for i, v in enumerate(self):
  624.             if v == value:
  625.                 return i
  626.         
  627.         raise ValueError
  628.  
  629.     
  630.     def count(self, value):
  631.         return (sum,)((lambda .0: for v in .0:
  632. if v == value:
  633. 1continue)(self))
  634.  
  635.  
  636. Sequence.register(tuple)
  637. Sequence.register(basestring)
  638. Sequence.register(buffer)
  639. Sequence.register(xrange)
  640.  
  641. class MutableSequence(Sequence):
  642.     
  643.     def __setitem__(self, index, value):
  644.         raise IndexError
  645.  
  646.     __setitem__ = abstractmethod(__setitem__)
  647.     
  648.     def __delitem__(self, index):
  649.         raise IndexError
  650.  
  651.     __delitem__ = abstractmethod(__delitem__)
  652.     
  653.     def insert(self, index, value):
  654.         raise IndexError
  655.  
  656.     insert = abstractmethod(insert)
  657.     
  658.     def append(self, value):
  659.         self.insert(len(self), value)
  660.  
  661.     
  662.     def reverse(self):
  663.         n = len(self)
  664.         for i in range(n // 2):
  665.             self[i] = self[n - i - 1]
  666.             self[n - i - 1] = self[i]
  667.         
  668.  
  669.     
  670.     def extend(self, values):
  671.         for v in values:
  672.             self.append(v)
  673.         
  674.  
  675.     
  676.     def pop(self, index = -1):
  677.         v = self[index]
  678.         del self[index]
  679.         return v
  680.  
  681.     
  682.     def remove(self, value):
  683.         del self[self.index(value)]
  684.  
  685.     
  686.     def __iadd__(self, values):
  687.         self.extend(values)
  688.         return self
  689.  
  690.  
  691. MutableSequence.register(list)
  692.